import type { Istate } from '../AppProvider'
//action类型
export type Iaction = {
    type: string,
    [k: string]: any
}

export default (state: Istate, action: Iaction): Istate => {
    let { type, payload } = action;
    switch (type) {
        case 'ADD_TODO':
            let newData = [{ ...payload }, ...state.data];
            return { ...state, data: newData, copyData: newData }
        case 'DELETE_TODO':
            let newData2 = state.data.filter(item => item.id !== payload)
            return { ...state, data: newData2, copyData: newData2 }
        case "CHANGE_TODO":
            let { targetid, checked } = payload;
            //筛选之后的动态数据
            let targetItem = state.data.find(item => item.id ==
                targetid)
            targetItem && (targetItem.completed = checked)
            console.log(state.data)

            //copyData是原始数据
            let targetItem2 = state.copyData!.find(item => item.id == targetid);
            targetItem2 && (targetItem2.completed = checked)
            //返回新的状态
            return { ...state, data: [...state.data], copyData: [...state.copyData!] }
        case "CHANGE_STATUS":
            if (payload === 'completed') {
                return { ...state, data: state.copyData!.filter(item => item.completed), activeButton: payload }
            } else if (payload === 'uncompleted') {
                return { ...state, data: state.copyData!.filter(item => !item.completed),activeButton:payload }
            } else {
                return { ...state, data: [...state.copyData!],activeButton:payload }
            }
        default:
            return { ...state }
    }
}